gdk: Add GdkContentFormatsBuilder
authorBenjamin Otte <otte@redhat.com>
Mon, 20 Nov 2017 01:47:45 +0000 (02:47 +0100)
committerBenjamin Otte <otte@redhat.com>
Mon, 20 Nov 2017 22:12:48 +0000 (23:12 +0100)
Yes, the name is too long.
No, I couldn't think of a shorter one.

docs/reference/gdk/gdk4-sections.txt
gdk/gdkcontentformats.c
gdk/gdkcontentformats.h

index 2752f03b1873487f4565ab222c26b8af64976ffe..1746f1983b0685d77017c3e385951e37dac0dac7 100644 (file)
@@ -370,6 +370,7 @@ gdk_fullscreen_mode_get_type
 <SECTION>
 <FILE>gdkcontentformats</FILE>
 <TITLE>Content Formats</TITLE>
+GdkContentFormats
 gdk_content_formats_new
 gdk_content_formats_ref
 gdk_content_formats_unref
@@ -380,6 +381,14 @@ gdk_content_formats_union
 gdk_content_formats_intersects
 gdk_content_formats_remove
 gdk_content_formats_contains
+
+<SUBSECTION>
+GdkContentFormatsBuilder
+gdk_content_formats_builder_new
+gdk_content_formats_builder_free
+gdk_content_formats_builder_add_formats
+gdk_content_formats_builder_add_mime_type
+
 <SUBSECTION Private>
 gdk_content_formats_get_type
 </SECTION>
index c3cd9cde633b71a60b827435bb7e6ccd344e4bac..bb1496426c34b1ba934b41215c91e31afe6443e2 100644 (file)
  * For debugging purposes, the function gdk_content_formats_to_string() exists.
  * It will print a comma-seperated formats of formats from most important to least
  * important.
+ *
+ * #GdkContentFormats is an immutable struct. After creation, you cannot change
+ * the types it represents. Instead, new #GdkContentFormats have to be created.
+ * The #GdkContentFormatsBuilder structure is meant to help in this endeavor.
  */
 
 /**
@@ -310,3 +314,107 @@ gdk_content_formats_get_atoms (GdkContentFormats *formats,
   return atoms;
 }
 
+/**
+ * GdkContentFormatsBuilder:
+ *
+ * A #GdkContentFormatsBuilder struct is an opaque struct. It is meant to
+ * not be kept around and only be used to create new #GdkContentFormats
+ * objects.
+ */
+
+struct _GdkContentFormatsBuilder
+{
+  GSList *mime_types;
+  gsize n_mime_types;
+};
+
+/**
+ * gdk_content_formats_builder_new:
+ *
+ * Create a new #GdkContentFormatsBuilder object. The resulting builder
+ * would create an empty #GdkContentFormats. Use addition functions to add
+ * types to it.
+ *
+ * Returns: a new #GdkContentFormatsBuilder
+ **/
+GdkContentFormatsBuilder *
+gdk_content_formats_builder_new (void)
+{
+  return g_slice_new0 (GdkContentFormatsBuilder);
+}
+
+/**
+ * gdk_content_formats_builder_free:
+ * @builder: a #GdkContentFormatsBuilder
+ *
+ * Frees @builder and creates a new #GdkContentFormats from it.
+ *
+ * Returns: a new #GdkContentFormats with all the formats added to @builder
+ **/
+GdkContentFormats *
+gdk_content_formats_builder_free (GdkContentFormatsBuilder *builder)
+{
+  GdkContentFormats *result;
+  const char **mime_types;
+  GSList *l;
+  gsize i;
+
+  g_return_val_if_fail (builder != NULL, NULL);
+
+  mime_types = g_new (const char *, builder->n_mime_types + 1);
+  i = builder->n_mime_types;
+  mime_types[i--] = NULL;
+  /* add backwards because most important type is last in the list */
+  for (l = builder->mime_types; l; l = l->next)
+    mime_types[i--] = l->data;
+
+  result = gdk_content_formats_new (mime_types, builder->n_mime_types);
+  g_free (mime_types);
+
+  return result;
+}
+
+/**
+ * gdk_content_formats_builder_add_formats:
+ * @builder: a #GdkContentFormatsBuilder
+ * @formats: the formats to add
+ *
+ * Appends all formats from @formats to @builder, skipping those that
+ * already exist.
+ **/
+void
+gdk_content_formats_builder_add_formats (GdkContentFormatsBuilder *builder,
+                                         GdkContentFormats        *formats)
+{
+  GList *l;
+
+  g_return_if_fail (builder != NULL);
+  g_return_if_fail (formats != NULL);
+
+  for (l = formats->formats; l; l = l->next)
+    gdk_content_formats_builder_add_mime_type (builder, l->data);
+}
+
+/**
+ * gdk_content_formats_builder_add_formats:
+ * @builder: a #GdkContentFormatsBuilder
+ * @mime_type: a mime type
+ *
+ * Appends @mime_type to @builder if it has not already been added.
+ **/
+void
+gdk_content_formats_builder_add_mime_type (GdkContentFormatsBuilder *builder,
+                                           const char               *mime_type)
+{
+  g_return_if_fail (builder != NULL);
+  g_return_if_fail (mime_type != NULL);
+
+  mime_type = g_intern_string (mime_type);
+
+  if (g_slist_find (builder->mime_types, mime_type))
+    return;
+
+  builder->mime_types = g_slist_prepend (builder->mime_types, (gpointer) mime_type);
+  builder->n_mime_types++;
+}
+
index f73c5b46886c725e600305a8ab9ec76971a149f9..5b27a476548ba5b6ea73d2d8d89ffc811bd2ab13 100644 (file)
@@ -59,6 +59,19 @@ GDK_AVAILABLE_IN_3_94
 gboolean                gdk_content_formats_contains            (const GdkContentFormats        *formats,
                                                                  const char                     *mime_type);
 
+typedef struct _GdkContentFormatsBuilder GdkContentFormatsBuilder;
+
+GDK_AVAILABLE_IN_3_94
+GdkContentFormatsBuilder*gdk_content_formats_builder_new        (void);
+GDK_AVAILABLE_IN_3_94
+GdkContentFormats *     gdk_content_formats_builder_free        (GdkContentFormatsBuilder       *builder) G_GNUC_WARN_UNUSED_RESULT;
+GDK_AVAILABLE_IN_3_94
+void                    gdk_content_formats_builder_add_formats (GdkContentFormatsBuilder       *builder,
+                                                                 GdkContentFormats              *formats);
+GDK_AVAILABLE_IN_3_94
+void                    gdk_content_formats_builder_add_mime_type(GdkContentFormatsBuilder      *builder,
+                                                                 const char                     *mime_type);
+
 G_END_DECLS
 
 #endif /* __GTK_CONTENT_FORMATS_H__ */